<template>
  <view>
    <view class="search-box">
      <my-search @click.native="gotoSearch"></my-search>
    </view>
    <!-- // 轮播图的区域 -->
    <swiper
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="1000"
      circular="true"
    >
      <swiper-item v-for="item in swiperList" :key="goods_id">
        <navigator
          class="swiper-item"
          :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"
        >
          <image :src="item.image_src" class="nav-img"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 首页分类选项的区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, index) in navList" :key="index">
        <image
          :src="item.image_src"
          class="nav-img"
          @click="navClickHandler(item)"
        ></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 每一个楼层的item项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层的标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-image-box">
          <!-- 左侧大图片的盒子 -->
          <navigator class="left-image-box" :url="item.product_list[0].url">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
            ></image>
          </navigator>
          <!-- 右侧4张小图片的盒子 -->
          <view class="right-image-box">
            <navigator
              class="right-img-item"
              v-for="(item2, index2) in item.product_list"
              :key="index2"
              v-show="index2 !== 0"
              :url="item2.url"
            >
              <image
                :src="item2.image_src"
                mode="widthFix"
                :style="{ width: item2.image_width + 'rpx' }"
              ></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import badgeNux from '@/mixins/tabbar-badge.js';
export default {
  mixins: [badgeNux],
  data() {
    return {
      // 轮播图数据
      swiperList: [],
      // 分类导航数据
      navList: [],
      // 楼层数据列表
      floorList: []
    };
  },
  onLoad() {
    // 调用方法获取轮播图的数据
    this.getSwiperList();
    // 调用方法获取首页分类选项数据
    this.getNavList();
    // 获取楼层列表的数据
    this.getFloorList();
  },
  methods: {
    async getSwiperList() {
      let result = await uni.$http.get('/api/public/v1/home/swiperdata');
      if (result.data.meta.status == 200) {
        this.swiperList = result.data.message;
        return uni.$showMsg('数据请求成功');
      } else {
        // return uni.showToast({
        //   title: '请求失败了,请稍后再尝试',
        //   duration: 1500,
        //   icon: 'none'
        // });
        return uni.$showMsg();
      }
    },
    async getNavList() {
      let result = await uni.$http.get('/api/public/v1/home/catitems');
      if (result.data.meta.status == 200) {
        this.navList = result.data.message;
      } else {
        return uni.$showMsg();
      }
    },
    // 点击分类跳转的方法
    navClickHandler(item) {
      switch (item.name) {
        case '分类':
          uni.switchTab({
            url: '/pages/cate/cate'
          });
          break;
        case '秒杀拍':
          uni.switchTab({
            url: '/pages/cate/cate'
          });
          break;
        default:
          break;
      }
    },
    async getFloorList() {
      let result = await uni.$http.get('/api/public/v1/home/floordata');
      if (result.data.meta.status == 200) {
        // 对数据进行处理
        result.data.message.forEach(item => {
          item.product_list.forEach(item2 => {
            item2.url =
              '/subpkg/goods_list/goods_list?' +
              item2.navigator_url.split('?')[1];
          });
        });
        this.floorList = result.data.message;
      } else {
        return uni.$showMsg();
      }
    },
    // 跳转到搜索页面
    gotoSearch() {
      uni.navigateTo({
        url: '/subpkg/search/search'
      });
    }
  }
};
</script>

<style lang="scss">
swiper {
  height: 330rpx;
  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;
  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-image-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-image-box {
  display: flex;
  padding-left: 14rpx;
}

.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>
